package com.elinkway.infinitemovies.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.RankHomeData;
import com.elinkway.infinitemovies.bean.RankItem;
import com.elinkway.infinitemovies.bean.RankList;
import com.elinkway.infinitemovies.bigdata.BigDataApi;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.bigdata.RealDataApi;
import com.elinkway.infinitemovies.listener.MyOnClickListener;
import com.elinkway.infinitemovies.ui.activity.RankDetailActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Map;

public class RankHomeGridAdapter extends MyBaseAdapter {

	private Activity mActivity;
	private RankHomeData rankData;

	public RankHomeGridAdapter(Context context,
			RankHomeData data) {
		super(context);
		this.rankData = data;
		mActivity = (Activity)context;
		// TODO Auto-generated constructor stub
	}

	private class ItemHolder {
		TextView tv_name;
		TextView tv_firstItemTitle;
		RelativeLayout rl_channelButton;
		RelativeLayout rl_firstItem;
		ImageView img_cover;
		LinearLayout subList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		RankList rankList = new RankList();
		switch(position){
			case 0:
				rankList=rankData.getMovieRank();
				break;
			case 1:
				rankList=rankData.getTvRank();
				break;
			case 2:
				rankList=rankData.getCartoonRank();
				break;
			case 3:
				rankList=rankData.getZongyiRank();
				break;
		}
		ItemHolder item;

		// subList的数据, 排名在1之后的数据

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.rank_home_item, null);
			item = new ItemHolder();
			item.tv_name = (TextView) convertView
					.findViewById(R.id.rank_item_name);
			item.tv_firstItemTitle = (TextView) convertView
					.findViewById(R.id.rank_first_item_title);
			item.img_cover = (ImageView) convertView
					.findViewById(R.id.rank_first_item_cover);
			item.subList = (LinearLayout) convertView
					.findViewById(R.id.rank_item_list);
			item.rl_channelButton = (RelativeLayout) convertView
					.findViewById(R.id.rank_channel_enter);
			item.rl_firstItem = (RelativeLayout) convertView
					.findViewById(R.id.rank_first_item);
			convertView.setTag(item);
		} else {
			item = (ItemHolder) convertView.getTag();
			item.subList.removeAllViews();
		}
		item.tv_name.setText(rankList.getName());
//		item.img_cover.setImageResource((Integer) map.get("cover"));

		// 获取第一项，第一项内容置入头部项目
		item.tv_firstItemTitle.setText("1."+rankList.getRankList().get(0).getName());
		ImageLoader.getInstance().displayImage(rankList.getRankList().get(0).getPic(), item.img_cover);

		for (int i = 1; i < rankList.getRankList().size(); i++) {
			item.subList.addView(initSubListText(i+1, rankList.getRankList().get(i), rankList.getVt()));
			if(i==rankList.getRankList().size()-1) {
				continue;
			}
			View divider =new View(mActivity);
			divider.setBackgroundResource(R.drawable.rank_divider);
			item.subList.addView(divider);
		}
		final String vt=rankList.getVt();
		// 顶部channel入口点击事件绑定
		item.rl_channelButton.setOnClickListener(new MyOnClickListener() {

			@Override
			public void onClickListener(View v) {
				// TODO Auto-generated method stub
				RankDetailActivity.launch(mActivity,vt);
			}
		});

		// 第一项入口点击事件绑定
		item.rl_firstItem.setOnClickListener(new VideoNameOnClickListener((rankList.getRankList().get(0)), vt));

		return convertView;
	}

	private TextView initSubListText(int rank, RankItem rankItem, String vt) {
		StringBuilder sb = new StringBuilder();
		// position+2 得到“2. text_content”效果
		sb.append(rank);
		sb.append(".");
		sb.append((String) rankItem.getName());
		
		TextView tv_content = new TextView(mContext);
		tv_content.setTextSize((float) 15.0);
		tv_content.setSingleLine();
		tv_content.setEllipsize(TruncateAt.END);
		tv_content.setText(sb.toString());
		tv_content.setGravity(Gravity.CENTER | Gravity.LEFT);
		tv_content.setPadding(0, 0, mContext.getResources()
				.getDimensionPixelSize(R.dimen.len_15dp), 0);
		tv_content
				.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
						mContext.getResources().getDimensionPixelSize(
								R.dimen.len_35dp)));
		tv_content.setTextColor(mContext.getResources().getColor(
				R.color.black_000000));
		tv_content.setOnClickListener(new VideoNameOnClickListener(rankItem, vt));
		tv_content.setClickable(true);
		tv_content.setBackgroundResource(R.drawable.rank_sublist_selector);
		return tv_content;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return rankData.getSize();
	}
	
	// 点击视频名的事件
	private class  VideoNameOnClickListener implements OnClickListener{
		private RankItem item;
		private String vt;
		
		public VideoNameOnClickListener(RankItem item, String vt) {
			super();
			this.item = item;
			this.vt = vt;
		}

		@Override
		public void onClick(View v) {
			Map<String, String> dataMap = BigDataApi.createDataMap("0", "vt="+vt,"0","-",item.getAlbumid(),"-");
			dataMap.put("cur_url", BigDataConstants.PGV_CURRENT_ID_RANKING);
			BigDataApi.bigDataCommonReport(dataMap, mActivity);
			RealDataApi.RealDataClickReport("","",item.getAlbumid(),"","");

			VideoDetailActivity.launch(mActivity, item.getAlbumid(), item.getVt(), item.getName(), "", item.getThemeid(), "", "0","","");
		}
		 
	}
}